<template>
  <div>
    <el-main
        class="detail-box"
        v-loading="loading"
        element-loading-text="正在加载"
        element-loading-spinner="el-icon-loading">
      <div class="container-title">
        <img src="../../../assets/image/task-details.png" alt="" style="width: 150px">
      </div>
      <el-descriptions
          class="margin-top"
          :column="1"
          border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-edit"></i>
            测试名称
          </template>
          {{ form.taskname }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            发包方
          </template>
          {{ form.username }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-setting"></i>
            测试类型
          </template>
          {{ form.type }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-time"></i>
            截止日期
          </template>
          {{ form.deadline }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-document"></i>
            任务描述
          </template>
          {{ form.description }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <icon-font icon="icon-moneybagfill"></icon-font>
            报告单价
          </template>
          {{ form.reportPrice }}
        </el-descriptions-item>
      </el-descriptions>
      <device-requirements-view :value="form.deviceRequirement"></device-requirements-view>
      <el-button id="get-task-btn" type="primary" @click="getTask()" v-if="!got">
        领取任务
      </el-button>
      <el-button type="info" v-else-if="got" disabled>
        已领取
      </el-button>
    </el-main>

  </div>
</template>

<script>
import {getTaskDetailsByTaskID} from "@/api/task";
import {whetherClaimedCertainTask} from "@/api/worker";
import ClaimTask from "@/views/new/worker/claim-task/ClaimTask";
import DeviceRequirementsView from "@/components/new/device-requirements-card/DeviceRequirementsView";
import IconFont from "@/components/new/iconfont/IconFont";
export default {
  name: "subTaskInfo",
  props:["id"],
  components: {IconFont, DeviceRequirementsView, ClaimTask},
  data() {
    return {
      form: {
        taskname: '',
        environment: '',
        deadline: '',
        username: '',
        description: ''
      },

      got: false,

      taskId: -1,
      confirm: false
    };
  },

  computed: {
    loading() {
      return this.form['taskname'] === '';
    },

  },

  created() {
    this.getDetailObj();
    this.getTaskStatus();

  },

  methods: {

    // 组件挂载后，用于加载任务信息表格
    async getDetailObj() {
      const res = await getTaskDetailsByTaskID(this.$route.params.taskid);
      if (res.code !== 200) {
        return this.$message.error(res.msg);
      }

      for (let key of Object.keys(res.data)) {
        if (key !== 'type') {
          this.form[key] = res.data[key];
        } else {
          if (res.data['type'] === 'function_test') this.form['type'] = '功能测试';
          else if (res.data['type'] === 'performance_test') this.form['type'] = '性能测试';
          else if (res.data['type'] === 'bug_explore') this.form['type'] = 'Bug探索';
        }
      }
    },


    async getTaskStatus() {
      const workerid = sessionStorage.getItem('userid');
      const taskid = this.$route.params.taskid;
      const res = await whetherClaimedCertainTask(taskid, workerid);
      if (res.code === 200) {
        this.got = true;
      }
    },

    getTask() {
      this.$emit('acceptTask',this.id);
    },



  }
}
</script>

<style scoped>
.detail-box {
  margin: 10px auto;
  width: 700px;

  border: 1px solid #DCDFE6;
  box-shadow: 0 0 8px #909399;

  padding: 3% 3%;

  background-color: #ffffff;
}

</style>
